/*
 * Copyright (c) 2025 lhjhit
 *
 * SPDX-License-Identifier: Apache-2.0
 */
// #include <inttypes.h>

#include <zephyr/kernel.h>

#include <zephyr/logging/log.h>
// #include <zephyr/sys/printk.h>
// #include <zephyr/sys/util.h>
#include <zephyr/device.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/drivers/spi.h>
#include <zephyr/drivers/stepper.h>
#include <zephyr/drivers/pwm.h>

#include "led.h"
#include "../modules/ti_drv8889/driver/drv8889.h"

LOG_MODULE_REGISTER(MAIN_MODULE, LOG_LEVEL_INF);

// #define STACK_SIZE 1024
// #define PRIORITY_ROTATION 2
// #define PRIORITY_ADC 4
// #define PRIORITY_BLE 3

// k_tid_t id_thread_rotation;
// K_THREAD_STACK_DEFINE(stack_area_rotation, STACK_SIZE);
// static struct k_thread thread_data_rotation;

// k_tid_t id_thread_adc;
// K_THREAD_STACK_DEFINE(stack_area_adc, STACK_SIZE);
// static struct k_thread thread_data_adc;

// k_tid_t id_thread_ble;
// K_THREAD_STACK_DEFINE(stack_area_ble, STACK_SIZE);
// static struct k_thread thread_data_ble;

// static const struct pwm_dt_spec servo_rotation = PWM_DT_SPEC_GET(DT_NODELABEL(red_pwm_led));
// static const struct pwm_dt_spec pwm_led0 = PWM_DT_SPEC_GET(DT_ALIAS(pwm_led0));

// static const struct spi_dt_spec spi_dev = SPI_DT_SPEC_GET(
// 											SPI_NODE, 
//                        						SPI_OP_MODE_MASTER | SPI_WORD_SET(16) | SPI_TRANSFER_MSB | SPI_MODE_CPHA,
// 											0
// 											);

// (SPI_WORD_SET(8) | SPI_TRANSFER_MSB | SPI_MODE_CPOL | SPI_MODE_CPHA)

// cycles/min = (pulses/second * step_degree) / (6 * micro_step)
// pulses/second = (cycles/min * micro_step * 6) / step_degree
// microstep_interval = 10^9 / pulses/second


int main(void)
{
	static const struct device *stepper0 = DEVICE_DT_GET_ONE(ti_drv8889);	
	
	// stepper_disable(stepper0);

	if (led_setup()) return 0;
	blink_start();

	// if (!pwm_is_ready_dt(&pwm_led0)) {
	// 	LOG_ERR("Error: PWM device %s is not ready", pwm_led0.dev->name);
	// 	return -1;
	// }
	drv8889_set_trq_dac(stepper0, 10);
	stepper_set_micro_step_res(stepper0, STEPPER_MICRO_STEP_128);
	// stepper_set_microstep_interval(stepper0, 234411);
	stepper_set_microstep_interval(stepper0, 1000000);
	stepper_enable(stepper0);
	// stepper_disable(stepper0);
	stepper_run(stepper0, STEPPER_DIRECTION_POSITIVE);
	// stepper_run(stepper0, STEPPER_DIRECTION_NEGATIVE);
	// k_msleep(10);
	// pwm_set_pulse_dt(&pwm_led0, PWM_USEC(190));
	
	// k_msleep(10000);
	// stepper_stop(stepper0);
	// if(!device_is_ready(spi_dev)) {
	// 	LOG_ERR("SPI2 controller is not ready");
	// } else {
		// while(true) {
		// 	k_msleep(1000);
		// 	LOG_INF("SPI2 controller is ready");
		// // spi_transceive(spi_dev, &cfg, &tx, &rx);
		// 	drv8889_get_chipid(spi_dev, &value);
		// 	LOG_INF("SPI read done");
		// 	LOG_INF("Chip ID is %x", value);
		// // LOG_HEXDUMP_INF(&rx_data, sizeof(rx_data), "rx data:");
		// }
	// 	// spi_transceive_dt(&spi_dev, &tx, &rx);

	// }
	

	//TODO: use K_THREAD_DEFINE
	// id_thread_rotation = k_thread_create(&thread_data_rotation, 
	// 										stack_area_rotation, K_THREAD_STACK_SIZEOF(stack_area_rotation), 
	// 										thread_movement, 
	// 										NULL, NULL, NULL, 
	// 										PRIORITY_ROTATION, 0, K_FOREVER);

	// id_thread_adc = k_thread_create(&thread_data_adc, 
	// 									stack_area_adc, K_THREAD_STACK_SIZEOF(stack_area_adc), 
	// 									thread_adc, 
	// 									NULL, NULL, NULL, 
	// 									PRIORITY_ADC, 0, K_FOREVER);

	// id_thread_ble = k_thread_create(&thread_data_ble, 
	// 									stack_area_ble, K_THREAD_STACK_SIZEOF(stack_area_ble), 
	// 									thread_ble, 
	// 									NULL, NULL, NULL, 
	// 									PRIORITY_BLE, 0, K_FOREVER);

	// k_thread_name_set(&thread_data_rotation, "thread_rotation");
	// k_thread_name_set(&thread_data_adc, "thread_adc");
	// k_thread_name_set(&thread_data_ble, "thread_ble");

	// k_thread_start(&thread_data_rotation);
	// k_thread_start(&thread_data_adc);
	// k_thread_start(&thread_data_ble);
}